﻿using System.Linq;
using Ewk.MachineLearning.Classification.Common;
using Ewk.MachineLearning.Classification.Interfaces;
using Ewk.MachineLearning.Persistance.DataAccess;
using Ewk.MachineLearning.Persistance.Domain;

namespace Ewk.MachineLearning.Classification.Conversions.EntityVectorFactories
{
    /// <summary>
    /// Creates a vector representation of a <see cref="Document"/>.
    /// </summary>
    class DocumentVectorFactory : IEntityVectorFactory
    {
        #region Implementation of IEntityFactory

        /// <summary>
        /// Creates a vector representation of the <see cref="Document"/>.
        /// </summary>
        /// <returns>A vector representation of the <see cref="Document"/>.</returns>
        public EntityVector<double>[] CreateEntityVectors()
        {
            using (var catalog = CatalogFactory.NewCatalog())
            {
                var documentRepository = new TextEntityRepository<Document>(catalog);
                var documents = documentRepository.Entities;

                var wordRepository = new TextEntityRepository<Word>(catalog);
                var words = wordRepository.Entities
                                          .Cast<IEntity>()
                                          .ToList();

                return documents
                    .Select(document =>
                            new EntityVector<double>(
                                document.Id.ToString(),
                                document.ToVector<Document, Word>(words)))
                    .ToArray();
            }
        }

        #endregion
    }
}